package com.bestcem.xm.qdes.controller;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.qdes.controller.param.QstructsCssParam;
import com.bestcem.xm.qdes.controller.param.QstructsTemplatesParam;
import com.bestcem.xm.qdes.controller.param.QstructsTemplatesParamAddGroup;
import com.bestcem.xm.qdes.controller.param.QstructsTemplatesParamEditGroup;
import com.bestcem.xm.qdes.controller.vo.DeletedCountVo;
import com.bestcem.xm.qdes.controller.vo.QstructsAttachmentsVo;
import com.bestcem.xm.qdes.controller.vo.QstructsTemplatesVo;
import com.bestcem.xm.qdes.service.QdesQstructService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Api(tags = "问卷编辑-自定义")
@RestController
@RequiredArgsConstructor
@RequestMapping("/qdes/qstructs")
public class CustomizeProjectController {

    private @NonNull QdesQstructService qdesQstructService;

    @ApiOperation(value = "新增Template&Script模板接口")
    @PostMapping("/{id:[a-fA-F0-9]{24}}/templates/")
    public XmResultJson<?> addTemplates(@PathVariable String id,
                                        @Validated(QstructsTemplatesParamAddGroup.class) @RequestBody QstructsTemplatesParam qstructsParam) {
        return XmResultJson.success(qdesQstructService.addTemplates(id, qstructsParam));
    }

    @ApiOperation(value = "编辑模板保存接口")
    @PutMapping("/{id:[a-fA-F0-9]{24}}/templates/{tid:[a-fA-F0-9]{24}}")
    public XmResultJson<Object> editTemplates(@PathVariable String id,
                                              @PathVariable String tid,
                                              @Validated(QstructsTemplatesParamEditGroup.class) @RequestBody QstructsTemplatesParam qstructsParam) throws Exception {
        qdesQstructService.editTemplates(id, tid, qstructsParam);
        return XmResultJson.success(new HashMap<>());
    }

    @ApiOperation(value = "删除模板接口")
    @DeleteMapping("/{id:[a-fA-F0-9]{24}}/templates/{tid:[a-fA-F0-9]{24}}")
    public XmResultJson<DeletedCountVo> deleteTemplates(@PathVariable String id,
                                                        @PathVariable String tid) {
        return XmResultJson.success(qdesQstructService.deleteTemplates(id, tid));
    }

    @ApiOperation(value = "获取模板接口")
    @GetMapping("/{id:[a-fA-F0-9]{24}}/templates/")
    public XmResultJson<List<QstructsTemplatesVo>> getTemplates(@PathVariable String id,
                                                                @ApiParam(value = "模板名称模糊查询")
                                                                @RequestParam(value = "search") String search) {
        return XmResultJson.success(qdesQstructService.getTemplates(id, search));
    }

    @ApiOperation(value = "获取模板编辑接口")
    @GetMapping("/{id:[a-fA-F0-9]{24}}/templates/{tid:[a-fA-F0-9]{24}}")
    public XmResultJson<QstructsTemplatesVo> getEditTemplates(@PathVariable String id,
                                                              @PathVariable String tid) {
        return XmResultJson.success(qdesQstructService.getEditTemplates(id, tid));
    }

    @ApiOperation(value = "新增Css模板接口")
    @PostMapping("/{id:[a-fA-F0-9]{24}}/gcss")
    public XmResultJson<Map> addCss(@PathVariable String id,
                                    @Validated @RequestBody QstructsCssParam cssParam) throws Exception {
        return XmResultJson.success(qdesQstructService.addCss(id, cssParam));
    }

    @ApiOperation(value = "获取css模板接口")
    @GetMapping("/{id:[a-fA-F0-9]{24}}/gcss/")
    public XmResultJson<Map> getCss(@PathVariable String id) {
        return XmResultJson.success(JSON.parseObject(qdesQstructService.getById(id).getGcss()));
    }

    /**
     * 编辑文件，附件上传功能
     *
     * @param file 附件
     * @return
     * @author gy.zhang
     * @date 2022/8/31
     */
    @ApiOperation(value = "附件上传功能接口")
    @PostMapping("/{id:[a-fA-F0-9]{24}}/attachments/")
    public XmResultJson<Map> uploadImagelib(@PathVariable String id,
                                            @RequestParam("attachment") MultipartFile file,
                                            @RequestParam(name = "keep_ext") Boolean keepext) throws Exception {
        if (Validator.isNull(file)) {
            return XmResultJson.fail(4, ":attachment is required");
        }
        return XmResultJson.success(qdesQstructService.uploadFileInfo(id, file, keepext));
    }

    @ApiOperation(value = "获取附件接口")
    @GetMapping("/{id:[a-fA-F0-9]{24}}/attachments/")
    public XmResultJson<List<QstructsAttachmentsVo>> getAttachments(@PathVariable String id,
                                                                    @ApiParam(value = "文件名称模糊查询")
                                                                    @RequestParam(value = "search") String search) {
        return XmResultJson.success(qdesQstructService.getAttachments(id, search));
    }

    @ApiOperation(value = "删除附件接口")
    @DeleteMapping("/{id:[a-fA-F0-9]{24}}/attachments/{tid:[a-fA-F0-9]{24}}")
    public XmResultJson<DeletedCountVo> deleteAttachments(@PathVariable String id,
                                                          @PathVariable String tid) {
        return XmResultJson.success(qdesQstructService.deleteAttachments(id, tid));
    }

    @ApiOperation(value = "设置问卷外观接口")
    @PutMapping("/{id:[a-fA-F0-9]{24}}/outward")
    public XmResultJson<String> putOutward(@PathVariable String id,
                                           @Validated @RequestBody Map outward) {
        return XmResultJson.success(qdesQstructService.putOutward(id, outward));
    }

    @ApiOperation(value = "获取隐私接口")
    @GetMapping("/{id:[a-fA-F0-9]{24}}/policys/")
    public XmResultJson<List<Object>> getPolicys(@PathVariable String id) {
        return XmResultJson.success(JSONArray.parseArray(qdesQstructService.getById(id).getPolicy(), Object.class));
    }

    @ApiOperation(value = "获取隐私接口")
    @PostMapping("/{id:[a-fA-F0-9]{24}}/policys")
    public XmResultJson<List<Object>> postPolicys(@PathVariable String id,
                                                  @Validated @RequestBody List<Map> policyParam) {
        return XmResultJson.success(qdesQstructService.postPolicys(id, policyParam));
    }

    @ApiOperation(value = "获取隐私接口")
    @PutMapping("/{id:[a-fA-F0-9]{24}}/policys")
    public XmResultJson<Map> putPolicys(@PathVariable String id,
                                        @Validated @RequestBody Map policyParam) throws Exception {
        return XmResultJson.success(qdesQstructService.putPolicys(id, policyParam));
    }
}
